#include "service_arch/service_arch.h"
#include "service_arch/service_base_impl.h"
#include "database/simple_db_pool.h"
#include "base/logging.h"

#include "ArticleComment_types.h"
#include "ArticleComment_constants.h"
#include "ArticleComment.h"

#include <iostream>
#include <map>
#include <boost/detail/atomic_count.hpp>

#include <thrift/concurrency/Mutex.h>
#include <thrift/concurrency/TimerManager.h>

#include "article_comment/comment_redis_adapter.h"

using namespace std;
using namespace apache::thrift;

namespace jztzp {
class ReloadRedisTask;

class ArticleCommentHandler : public ArticleCommentIf, public ServiceBaseImpl {
  friend class ReloadRedisTask;
 public:
  ArticleCommentHandler();

  virtual ~ArticleCommentHandler();

  virtual void Post(const CommentItem& item);

  virtual void SetAuditState(const int64_t id, const AuditState::type audit);

  virtual void Delete(const int64_t id);

  virtual void GetById(CommentItem& _return, const int64_t id);

  virtual void GetComments(CommentListResult& _return, 
      // const AuditState::type audit,
      const int32_t audit_flag,
      const int32_t offset, 
      const int32_t limit);

  virtual void GetUserComments(CommentListResult& _return, 
      const int64_t sender_id, 
      const int32_t offset, 
      const int32_t limit);

  virtual int32_t GetArticleCommentCount(const int64_t article_id);

  virtual void GetArticleComments(CommentListResult& _return, 
      const int64_t article_id, 
      const int32_t offset, 
      const int32_t limit);
  bool ReloadRedis();
 protected:
  void CommentDbToThrift(const mysqlpp::Row & row, CommentItem * item);
  int64_t GenNextCommentId();
  int GetCommentById(CommentItem * comment, int32_t id);

 private:
  boost::detail::atomic_count comment_id_offset_;
  int64_t base_commnet_id_;

  base::SimpleConnectionPool db_pool_; 
  RedisAdapter redis_adapter_;

  concurrency::TimerManager timer_manager_;
};

}

